package com.zzxy.springSun.factory;

import com.zzxy.springSun.aspect.MyAspect;
import com.zzxy.springSun.service.impl.IStudentService;
import com.zzxy.springSun.service.impl.StudentServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {
    public static IStudentService createStudentService() {
        //1.target classs
        StudentServiceImpl studentService=new StudentServiceImpl();
        //2.advice
        MyAspect myAspect= new MyAspect();
        //3.weaving
        IStudentService studentServicePorxy=(IStudentService)Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), studentService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                myAspect.before();
                Object obj=method.invoke(studentService, args);
                myAspect.after();
                return obj;
            }
        });
        return studentServicePorxy;
    }
}
